<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <%@ include file="../../common_header.jsp" %>
    <script type="text/javascript">
    //所有的子窗体都需要有一个save函数，当点击弹出窗体的保存按钮时所进行的操作
    function btnSave_click()
    {
        var fun_ret = false;

        var nodeSelect = $('#treeModule').tree('getChecked');
        var ids=",";
        for(var i=0;i<nodeSelect.length;i++){
            var pnode =  $('#treeModule').tree('getParent',nodeSelect[i].target);
            if(pnode != null)
            {
                var pid = pnode.id;
                if(ids.indexOf(","+pid+",") < 0)
                {
                    ids = ids + pid + ",";
                }
                var ppnode =  $('#treeModule').tree('getParent',pnode.target);
                if(ppnode != null)
                {
                    var ppid = ppnode.id;
                    if(ids.indexOf(","+ppid+",") < 0)
                    {
                        ids = ids + ppid + ",";
                    }
                }
            }
            ids = ids + nodeSelect[i].id + ",";
        }
        $('#mids').val(ids);
        var data = $('#formMG').serialize();
        var url = '${ctx}/manage/sysRoles/saveAuthority';
        $.ajax({
            async: false, 
            type: "POST", 
            url : url,
            data:data,
            cache:false,
            dataType : 'json', 
            success : function(data) { 
                if(data.success!=0)
                {
                    top.Ext.Msg.alert("提示",data.msg);
                }
                else
                {
                    fun_ret = true;
                }
            } 
        });
        return fun_ret;
    }
    
    $(document).ready(function(){
    	$('#treeModule').tree({onLoadSuccess:function(){
            var url = "${ctx}/manage/sysRoles/getRoleMenu?gid=${id }";  //获取当前用户组所具有的模块信息
            
            $.getJSON(url, function(json){
            	var str= new Array();
            	str=json.obj.split(",");
                $.each(str,function(){
                    var node = $('#treeModule').tree('find', this);//查找节点  
                    if(node != null)
                    {
                        var sub =  $('#treeModule').tree('getChildren',node.target);
                        if(sub.length == 0)
                        {
                            $('#treeModule').tree('check', node.target);//将得到的节点选中 
                        }
                        
                    }
                        
                    
                });
            });             

    	}});

    });
    
    //将节点全部选中
    function treeChecked(selected, treeMenu) {
		var roots = $('#' + treeMenu).tree('getRoots');//返回tree的所有根节点数组  
		if (selected.checked) {
			for (var i = 0; i < roots.length; i++) {
				var node = $('#' + treeMenu).tree('find', roots[i].id);//查找节点  
				$('#' + treeMenu).tree('check', node.target);//将得到的节点选中  
			}
		} else {
			for (var i = 0; i < roots.length; i++) {
				var node = $('#' + treeMenu).tree('find', roots[i].id);
				$('#' + treeMenu).tree('uncheck', node.target);
			}
		}
	}
    
    </script>
  </head>
  <body>
     <div id="cenTitle" data-options="region:'center',title:''" fit="true">
        <div class="easyui-panel" style="padding:0px" style="height:400px" >
        <input title="全选" type="checkbox"
					onClick="treeChecked(this,'treeModule')" />全选
            <ul id="treeModule" class="easyui-tree" data-options="url:'${ctxl}/manage/sysMenus/getPermissModule?type=3',method:'get',animate:true,checkbox:true" idField="id" treeField="text"></ul>
        </div>
    </div>
    <div >
        <form id="formMG" method="post">
            <input id="gid" type="hidden" name="gid" value="${id }">
            <input id="mids" type="hidden" name="mids">
        </form>
    </div>
    
  </body>
</html>
